package com.cml.domain.service;

import com.alibaba.fastjson.JSONObject;
import com.cml.common.constant.MessageType;
import com.cml.common.context.ServerContext;
import com.cml.common.dto.entity.ServiceInstance;
import com.cml.common.dto.message.ServiceHearbeatRequest;
import com.cml.common.dto.message.ServiceRegisterRequest;
import com.cml.domain.entity.DefaultMessage;
import com.cml.domain.entity.Message;
import com.cml.domain.entity.Solt;
import com.cml.infrastructure.ClientMessageListener;
import com.cml.infrastructure.MessageQueueManager;
import org.apache.log4j.Logger;

/**
 * 姓名：陈茂林
 * 时间：2023/11/22 17:12
 * 描述：
 */
public class ServiceInstanceReplicasManagerService {
    private ServerContext serverContext;

    Logger logger = Logger.getLogger(ServiceInstanceReplicasManagerService.class);
    public ServiceInstanceReplicasManagerService(ServerContext serverContext){
        this.serverContext = serverContext;
    }

    public void replicasSynStart(){
        new Listener().start();
    }

    class Listener extends Thread{
        @Override
        public void run() {
            while (true){
                try {
                    DefaultMessage message = (DefaultMessage) MessageQueueManager.takeCurServerNodeServiceInsReplicasSYNMessage();
                    if (MessageType.CLIENT_REQUEST_SERVICE_HEARBEAT_MESSAGE == message.getDefaultMessageType()) {
                        ServiceHearbeatRequest serviceHearbeatRequest = JSONObject.parseObject(message.getDefaultMessage(),ServiceHearbeatRequest.class);
                        logger.info("收到副本"+serviceHearbeatRequest.getIp()+":"+serviceHearbeatRequest.getPort()+"心跳消息");
                        ServiceInstance serviceInstance = new ServiceInstance.Builder()
                                .setServiceRegisterRequest(serviceHearbeatRequest)
                                .build();
                        Solt solt=serverContext.getSoltManagerService().getServiceNameSolt(serviceHearbeatRequest.getServiceName());
                        solt.hearbeat(serviceInstance);
                    }else if (MessageType.CLIENT_REPONSE_SERVICE_REGISTER_MESSAGE == message.getDefaultMessageType()) {
                        ServiceRegisterRequest serviceRegisterRequest= JSONObject.parseObject(message.getDefaultMessage(),ServiceRegisterRequest.class);
                        logger.info("收到副本"+serviceRegisterRequest.getIp()+":"+serviceRegisterRequest.getPort()+"注册消息");

                        ServiceInstance serviceInstance = new ServiceInstance.Builder()
                                .setServiceRegisterRequest(serviceRegisterRequest)
                                .build();
                        Solt solt=serverContext.getSoltManagerService().getServiceNameSolt(serviceInstance.getServiceName());
                        solt.registerServiceInstance(serviceInstance);
                    }
                } catch (InterruptedException e) {
                    logger.error(e.getMessage(),e);
                }
            }
        }
    }
}
